<?php

namespace Src\utils;

class ResponseUtils
{

    public static function withJson($response, array $data, int $code)
    {
        $data = json_encode($data);
        $response->getBody()->write($data);
        $response = $response->withStatus($code);
        $response = $response->withHeader('Content-Type', 'application/json');
        return $response;
    }
    public static function ok($response, $data)
    {
        $resData = [
            'code' => 200,
            'success' => true,
            'message' => "",
            'data' => $data
        ];
        $resData = json_encode($resData);
        $response->getBody()->write($resData);
        $response = $response->withStatus(200);
        $response = $response->withHeader('Content-Type', 'application/json');
        return $response;
    }
    public static function okwithEnum($response, $data, array $msgEnum)
    {
        $resData = [
            'code' => $msgEnum['code'],
            'success' => true,
            'message' => $msgEnum['message'],
            'data' => $data
        ];
        $resData = json_encode($resData);
        $response->getBody()->write($resData);
        return $response->withStatus(200)->withHeader('Content-Type', 'application/json');
    }

    public static function okV1($response, $data = array())
    {
        $data = json_encode($data);
        $response->getBody()->write($data);
        return $response->withStatus(200)->withHeader('Content-Type', 'application/json');
    }
    public static function errorWithJson($response, $data = array())
    {
        $response = $response->getBody()->write(json_encode($data));
        return $response->withStatus(500)->withHeader('Content-Type', 'application/json');
    }
    public static function errorV1($response, string $message)
    {
        $data = [
            'code' => 500,
            'success' => false,
            'message' => $message
        ];
        $response->getBody()->write(json_encode($data));
        $response = $response->withStatus(500)->withHeader('Content-Type', 'application/json');
        return $response;
    }
    public static function error($response, string $message)
    {
        $data = [
            'code' => 500,
            'success' => false,
            'message' => $message
        ];
        $response->getBody()->write(json_encode($data));
        $response = $response->withStatus(500)->withHeader('Content-Type', 'application/json');
        return $response;
    }
    public static function errorwithEnum($response, array $exceptionenum)
    {
        $data = [
            'code' => $exceptionenum['code'],
            'success' => false,
            'message' => $exceptionenum['message']
        ];
        $response->getBody()->write(json_encode($data));
        $response = $response->withStatus($exceptionenum['code'])->withHeader('Content-Type', 'application/json');
        return $response;
    }
}